<?php
namespace Home\Controller;
use Think\Controller;
class UsersController extends Controller {
    
public function login(){

    $this->display();
}
public function reg(){//用户注册

$this->display();
       }

public function upload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     "./Public/Uploads/photo/"; // 设置附件上传根目录 
     $upload->autoSub = false;//关闭子目录，默认true
     $upload->saveName = 'time';
 //$upload->savePath  =     '__PUBLIC__/Admin/photo/'; // 设置附件上传(子)目录，默认目录名为年月日

        $info   =   $upload->upload();  // 上传文件 
        dump($info);
        if(!$info) {      
        $this->error($upload->getError());    }

        else{    
            // $this->success('上传成功！');
            return $info;  

}
}

public function register(){

 
$user=D("Users");

if($user->create()){
  if($_FILES['file']['error']==0) {
    $logoinfo=$this->upload();
    $pname=$logoinfo['file']['savename'];
  }else{
    $pname="defaule.jpg";
  }
  $user->photo=$pname;
   

    if(false !==$user->add()){
      $this->success('注册成功！');
    }else{$this->error('数据错误');
  }
}else{
  $this->error($user->getError());
}

}
public function thumb(){
    $image=new\Think\Image();
    $image->open('./1.jpg');
    //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
    $image->thumb(150,150)->save('./thumb.jpg');
      }




   
   public function _initialize(){
       //只要不是显示登录页面和处理登录、验证码这些，其他情况都要进行session验证。
      if(ACTION_NAME!='index' && ACTION_NAME!='checkLogin'&& ACTION_NAME!='verify'){
               //redirect('index');
           }
       }
       function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
     public function checkLogin(){
        $code=I('post.verify');  //获取用户在表单中输入的验证码字符串
    if(!$this->check_verify($code))$this->error('验证码错误'); //调用check_verify函数，将用户输入的验证码加密处理后和Session中已存的验证码进行比较

        $usermodel=M('users'); //实例化diuba_users表
        $map   =   array();
        $map['telephone'] =trim(I('post.telephone'));//获取用户输入的手机号
        $map['password']  =md5(trim(I('post.password')));//获取用户输入的密码，并用md5加密
       
        $res=$usermodel->where($map)->select();
        if($res==null){
            $this->error('账号或密码错误');
        }else{
        $_SESSION['telephone']=$map['telephone'];
        $_SESSION['alias']=$res[0]["aliasname"];
        redirect(U("Meishis/meishis"));
        }
        }
 
    public function verify(){

        ob_clean();
//        $config =    array(
//            'fontSize'    =>    30,    // 验证码字体大小
//            'length'      =>    3,     // 验证码位数
//            'useNoise'    =>    false, // 关闭验证码杂点
//            //'fontttf' => 'msyhbd.ttf',
//           
//
//            'useImgBg' => false,
//            //'useZh' => true,
//        );
//        $Verify = new \Think\Verify($config);
        $Verify = new \Think\Verify();
        $Verify->entry();
        
    }




public function userinfo(){
        $user = M('users');
        $data['telephone']=$_SESSION['telephone'];
        $res = $user->where($data)->find();
        $this->list=$res;

        $meishis = M('meishis');
        $findData['username'] = $data['telephone'];
        $Page = new \Think\Page($qrcodenum, 2);
        $Page->setConfig('header','共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp每页<b>2</b>条&nbsp;&nbsp第<b></b>页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->link = $Page->show();
        $this->qrcodeList = $meishis->where($findData)->
        order("id desc")->limit($Page->firstRow, 2)->select();

        $collection = M('collection');
        $findData['c_id'];
        $Page = new \Think\Page($qrcodenum, 2);
        $Page->setConfig('header','共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp每页<b>2</b>条&nbsp;&nbsp第<b></b>页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->link = $Page->show();
        $this->collectionList = $collection->where($findData)->
        order("c_id")->limit($Page->firstRow, 2)->select();

        $this->display();


    }

function editUser(){
        $user = M('users');
        $data['telephone']=$_SESSION['telephone'];
        $res = $user->where($data)->find();
        $this->list=$res;
        $this->display();
    }
    function doUpdate($telephone){
        $telephone = $_SESSION['telephone'];
        $usersmodel=M('users');
        if($_FILES['photo']['error'] == 0){
            $logoinfo = $this->upload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
        
        $data['aliasname']=I('post.aliasname');
       $data['telephone']=I('post.telephone');
        $data['email']=I('post.email');
        $data['photo'] = $pname;
        $res=$usersmodel->where("telephone = $telephone")->save($data);
        if (false!==$res){
            $this->success('信息修改成功','userinfo');
        } else{
            $this->error('信息修改失败');
        }
    }
 public function checkTele(){
        $t=$_GET["t"];
        $userModel = M("Users");
        $rs=$userModel->where("telephone='$t'")->select();
        if($rs) echo "手机号已存在";




    }

       
}